import 'dart:math';

import 'package:flutter/material.dart';
import 'package:top_foundation/utils/util.dart';

import '../../utils/theme/theme.dart';

class DialogChooseView extends StatefulWidget {
  const DialogChooseView({
    Key? key,
    required this.titles,
    required this.callback,
    this.initialIndexs,
    this.onCancel,
    this.onSure,
  }) : super(key: key);
  final List<String> titles;
  final void Function(List<int>) callback;
  final VoidCallback? onCancel;
  final VoidCallback? onSure;
  final List<int>? initialIndexs;
  @override
  State<DialogChooseView> createState() => _DialogChooseViewState();
}

class _DialogChooseViewState extends State<DialogChooseView> {
  late List<int> choosedIndexs;
  final textCtrl = TextEditingController();
  @override
  void initState() {
    choosedIndexs = widget.initialIndexs ?? [];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var titles = widget.titles
        .where((element) => element.contains(textCtrl.text))
        .toList();
    var height = 50.r * 10;

    return Container(
      height: height,
      padding: EdgeInsets.symmetric(horizontal: 15.r),
      child: SafeArea(
        child: Column(
          children: [
            Container(
              decoration: const BoxDecoration(
                border: Border(
                  bottom: BorderSide(color: Color(0xffdedede), width: 0.5),
                ),
              ),
              height: 44.r,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  TextButton(
                      onPressed: widget.onCancel,
                      child: Text(
                        '取消',
                        style: TextStyle(fontSize: 15.sp),
                      )),
                  TextButton(
                      onPressed: widget.onSure,
                      child: Text(
                        '确定',
                        style: TextStyle(color: Colors.blue, fontSize: 15.sp),
                      )),
                ],
              ),
            ),
            TextField(
              controller: textCtrl,
              style: const TextStyle(color: Color(0xff333333)),
              decoration: const InputDecoration(
                hintText: '搜索',
                hintStyle: TextStyle(color: Color(0xffbbbbbb)),
              ),
              onChanged: (value) {
                setState(() {});
              },
            ),
            Expanded(
              child: ListView.builder(
                itemBuilder: (context, index) {
                  var t = titles[index];
                  var isChoosed = choosedIndexs.contains(index);
                  return InkWell(
                    onTap: () {
                      if (isChoosed) {
                        choosedIndexs.remove(index);
                      } else {
                        choosedIndexs.add(index);
                      }
                      widget.callback(choosedIndexs);
                      setState(() {});
                    },
                    child: SizedBox(
                      height: 50.r,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            t,
                            style: TextStyle(
                                color: const Color(0xff333333),
                                fontSize: 16.sp),
                          ),
                          Icon(
                            isChoosed
                                ? Icons.check_circle
                                : Icons.circle_outlined,
                            size: 24.r,
                            color: isChoosed
                                ? ITheme.selectedColor
                                : const Color(0xff999999),
                          ),
                        ],
                      ),
                    ),
                  );
                },
                itemCount: titles.length,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
